package com.qf.service.stuServiceImple;

import com.qf.dao.StuDAO;
import com.qf.entity.Stu;
import com.qf.service.StuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.List;

//@Service //<bean id="xx" class="xx.x.x.StuServiceImpl" >
@Service("stuService2")//<bean id="userService" class="xx.x.x.UserServiceImpl">
@Scope("prototype")//多例bean
//类中的每个方法都切入事务(有自己的事务控制的方法除外)
@Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = Exception.class, timeout = -1)
public class StuServiceImpl implements StuService {
    //  @Autowired //类型 自动注入
    //   @Resource(name="userDAO") //名称自动注入
    //基类类型 自动注入 并挑选 beanid="stuDAO"
    @Autowired
    @Qualifier("stuDAO")
    private StuDAO stuDAO;//注入数据访问层

    public StuDAO getStuDAO() {
        return stuDAO;
    }

    public void setStuDAO(StuDAO stuDAO) {
        this.stuDAO = stuDAO;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<Stu> queryStus() {
        System.out.println("queryStu in service ");
      //  int a=10/0;
        return stuDAO.queryStus();
    }

    @Override
    public Integer insertStu(Stu stu) {
        return stuDAO.insertStu(stu);
    }

    @Override
    public Integer updateStu(Stu stu) {
        return stuDAO.updateStu(stu);
    }

    //    public Integer deleteStu(Integer id) {
//        return stuDAO.deleteStu(id);
//    }
    @Override
    public Integer deleteStu(Integer id) {
//        业务层调用数据访问层实现功能
        Integer integer = stuDAO.deleteStu(id);
        //     int a = 10 / 0;
//        if (1 == 1) {
//            try {
//                throw new SQLException("test事务");
//            } catch (SQLException throwables) {
//                throwables.printStackTrace();
//                throw new RuntimeException("test事务！");
//            }
//        }
        return integer;
    }
}
